package cn.gailvlun.gll.presentation.emotion.edit.emotionlib

import android.content.Intent
import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentStatePagerAdapter
import android.support.v4.view.ViewPager
import android.widget.TextView
import cn.gailvlun.gll.R
import cn.gailvlun.gll.databinding.ActivityEmotionLibBinding
import cn.gailvlun.gll.databinding.TabEmotionLibBinding
import cn.gailvlun.gll.net.emotion.SourceType
import cn.gailvlun.gll.presentation.base.BaseActivity
import io.reactivex.functions.Consumer
import rx_activity_result2.Result
import rx_activity_result2.RxActivityResult


class EmotionLibActivity : BaseActivity() {
    lateinit var mBinding: ActivityEmotionLibBinding
    lateinit var mPagerAdapter: EmotionLibPagerAdapter

    companion object {
        val tabTitles = arrayListOf("句子", "电影", "表情")
        fun openActivity(activity: BaseActivity, consumer: Consumer<Result<BaseActivity>>) {
            val intent = Intent(activity, EmotionLibActivity::class.java)
            RxActivityResult.on(activity)
                    .startIntent(intent)
                    .subscribe(consumer)
        }
    }

    override fun initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_emotion_lib)
        mPagerAdapter = EmotionLibPagerAdapter(supportFragmentManager)
        mBinding.vp.adapter = mPagerAdapter
        mBinding.vp.offscreenPageLimit = 2

        mBinding.stl.setCustomTabView { container, position, adapter ->
            val tabBinding = DataBindingUtil.inflate<TabEmotionLibBinding>(layoutInflater,
                    R.layout.tab_emotion_lib, container, false)
            tabBinding.tvTab.text = tabTitles[position]
            tabBinding.root
        }
        mBinding.stl.setViewPager(mBinding.vp)
        mBinding.vp.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                for (i in tabTitles.indices) {
                    val view = mBinding.stl.getTabAt(i)
                    val tv = view.findViewById<TextView>(R.id.tv_tab)
                    if (i == position) {
                        tv.setTextColor(resources.getColor(R.color.white))
                    } else {
                        tv.setTextColor(resources.getColor(R.color.emotion_palasky))
                    }
                }
            }
        })
    }

    override fun createHandler() {
        mBinding.ivBack.setOnClickListener { view ->
            onBackPressed()
        }
    }

    override fun loadData(savedInstanceState: Bundle?) {

    }

    inner class EmotionLibPagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) {
        override fun getItem(position: Int): Fragment? {
            return when (position) {
                0 -> {
                    EmotionLibFragment.getInstance(SourceType.S)
                }
                1 -> {
                    EmotionLibFragment.getInstance(SourceType.F)
                }
                2 -> {
                    EmotionLibFragment.getInstance(SourceType.E)
                }
                else -> {
                    null
                }
            }

        }

        override fun getCount(): Int {
            return 3
        }

    }
}